package com.fram.controller.api;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.UserReceivingAddress;
import com.fram.service.api.v1.UserAddressService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月27日 - 上午9:09:37 @description：-> 收货地址模块API
 */
@RestController
@RequestMapping("/api/{version}/auth/address")
public class UserAddressController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(UserAddressController.class);

	@Resource(name = "userAddressService")
	private UserAddressService userAddressService;

	/** 查询用户的收货地址 */
	@ApiVersion(1.0)
	@RequestMapping("/list")
	public ResponseMsg listAddress(Integer isDefault) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(userAddressService.selectAddressListByUserId(getApiUserId(), isDefault));
		} catch (ProException e) {
			LOG.error("-收货地址-查询用户的收货地址 ->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-收货地址-查询用户的收货地址 ->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 增加（）收货地址
	 * @param request
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg insertUserAddress(@RequestBody UserReceivingAddress request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			userAddressService.insertReceivingAddress(request);
		} catch (ProException e) {
			LOG.error("-收货地址-增加用户的收货地址->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-收货地址-增加用户的收货地址2->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 根据id 查询 收货地址详情信息
	 * @param id
	 * @return
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/detail")
	private ResponseMsg selectUserAddressById(Long id) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(userAddressService.selectAddressById(id));
		} catch (ProException e) {
			LOG.error("-收货地址-根据收货地址 id 查看 详情->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-收货地址-根据收货地址查询id 查看 详情-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 修改收货地址。
	 * @param request
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	private ResponseMsg updateUserAddress(@RequestBody UserReceivingAddress request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			userAddressService.updateReceivingAddress(request);
		} catch (ProException e) {
			LOG.error("-收货地址-修改收货地址->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-收货地址-修改收货地址--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 删除收货地址
	 * @param id
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/delete")
	private ResponseMsg deleteUserAddress(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			userAddressService.deleteAddressById(id);
		} catch (ProException e) {
			LOG.error("-收货地址-删除收货地址->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-收货地址-删除收货地址--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
